package com.wwz.activiti;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
 * <p>Description:从ACT_GE_BYTEARRAY表保存bpmn资源文件</p >
 * <p>Copyright: Copyright (c)2021</p >
 * <P>Created Date :2021-04-07</P>
 * <P>@version 1.0</P>
 */
public class QuerryBpmnFile {
    public static void main(String[] args) throws IOException {
        //1.得到ProcessEngine对象
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

        //2.得到repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();

        //3.得到查询器 ProcessDefinitionQuery
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        //4.设置查询条件
        ProcessDefinitionQuery holiday = processDefinitionQuery.processDefinitionKey("holiday");//参数是流程定义key

        //5.执行查询操作
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();

        //6.通过流程定义信息 得到部署ID
        String deploymentId = processDefinition.getDeploymentId();

        //7.通过repositoryService的方法，实现读取图片信息及bpmn文件信息（输入流）
        //getResourceAsStream() 1 部署Id  资源名称
        //InputStream pngIs = repositoryService.getResourceAsStream(deploymentId,processDefinition.getDiagramResourceName());

        InputStream bpmnIs = repositoryService.getResourceAsStream(deploymentId,processDefinition.getResourceName());

        //8.构建出OutputStream
        //OutputStream pngOs = new FileOutputStream("/Users/sailor/Desktop/"+processDefinition.getDiagramResourceName());

        OutputStream bpmnOs = new FileOutputStream("/Users/sailor/Desktop/"+processDefinition.getResourceName());

        //9.输入流 输出流转换 commons-io-xx.jar中的方法
        //IOUtils.copy(pngIs,pngOs);
        IOUtils.copy(bpmnIs,bpmnOs);

        //10.关闭流
        //pngIs.close();
        bpmnIs.close();
        //.close();
        bpmnOs.close();

    }
}
